<?php

namespace plugin\cms\app\controller\admin\cnf;

use core\controller\AdminBase;
use Exception;
use plugin\cms\app\bll\Model as BllModel;
use support\View;
use plugin\cms\app\bll\ModelField as BllModelField;
use app\bll\Datadict as BllDatadict;
use app\bll\ComponentElement as BllComponentElement;

/**
 * CMS 模型全局
 */
class Model extends AdminBase
{
    public function index()
    {
        $bllModel =  new BllModel();
        $models = $bllModel->select([0]);
        View::assign('models', $models);
        return $this->view();
    }


    /**
     * 字段管理
     *
     * @return void
     */
    public  function fieldmanage()
    {
        $bllModelField = new BllModelField();
        $bllModel = new BllModel();
        $modelinfo = $bllModel->find(request()->get('modelid'), 0);
        if (!$modelinfo) {
            throw new Exception('模型不存在');
        }
        if (is_post()) {
            try {
                $bllModelField->batchsave(
                    array_values(request()->post('field')),
                    $modelinfo
                );
            } catch (Exception $ex) {
                return $this->error($ex);
            }
            return $this->success();
        } else {

            $fields = $bllModelField->select(request()->get('modelid'));

            //获取表单元素
            $bllComponentElement = new BllComponentElement();
            $elements = $bllComponentElement->select();


            $bllDatadict = new BllDatadict();
            $dicts = $bllDatadict->select();

            View::assign('dicts', array_column($dicts, null, 'id'));
            View::assign('elements', array_column($elements, null, 'ident'));
            View::assign('fields', $fields);

            return $this->view(frame: false);
        }
    }

    public function add()
    {
        if (is_post()) {
            try {
                $bllModel =  new BllModel();
                $data              = request()->post();

                $bllModel->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function edit()
    {
        $bllModel = new BllModel();
        $modelid  = request()->input('modelid');
        if (is_post()) {
            try {
                $bllModel->edit($modelid, 0, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $modelinfo = $bllModel->find($modelid, 0);
            View::assign('modelinfo', $modelinfo);
            return  $this->view();
        }
    }


    /**
     * 删除模型
     *
     * @return void
     */
    public function delete()
    {
        try {
            $bllModel =  new BllModel();
            $bllModel->delete(request()->input('modelid'), 0);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
